package com.koron.css2.workflow;

import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.PubReceiptMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.workflow.bean.HistoryTaskMsgBean;
import com.koron.css2.workflow.bean.ProcessListBean;
import com.koron.css2.workflow.bean.ProcessListQueryBean;
import com.koron.css2.workflow.bean.ProcessStartBean;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorkFlowHistoryPrint implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		ProcessStartBean bean = JsonUtils.objectToPojo(req.getData(), ProcessStartBean.class);
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			//读取对应水司的流程平台应用密钥
			PubReceiptMapper mapper = factory.getMapper(PubReceiptMapper.class);
			String setCode = mapper.getWorkflowApp(userInfo.getCurWaterCode());
			String processCode = bean.getProcessCode();//单据类型
			List<HistoryTaskMsgBean> list = WorkflowImpl.workflowhistory(setCode, processCode, bean.getBillId());
			//时间转换
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//打印数据
			ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
			ProcessListQueryBean qbean = new ProcessListQueryBean();
			qbean.setBillId(bean.getBillId());
			qbean.setProcessCode(processCode);
			qbean.setType(6);
			qbean.setOperator(userInfo.getUserInfo().getCode());
			PageInfo<ProcessListBean> nextNode = WorkflowImpl.workflow(qbean);
			List<ProcessListBean> lastNode = nextNode.getList();
			//取最后一个节点的下一节点审批人
			String nextNodeName="流程已结束";
			String title = "";
			if(lastNode!=null && lastNode.size()>0) {
				ProcessListBean lastmsg = lastNode.get(0);
				if(StringUtils.isNotBlank(lastmsg.getCurNodeUsersName())) {
					nextNodeName=lastmsg.getCurNodeName()+"("+lastmsg.getCurNodeUsersName()+")";
				}
				title=lastmsg.getProcessName()+"("+lastmsg.getBillNo()+")";
			}
			//抽取转换数据
			for (HistoryTaskMsgBean msg : list) {
				
				Map<String, String> map = new HashMap<String, String>();
				map.put("nowNodeName", msg.getNowNodeName());
				map.put("operationUserName", msg.getOperationUserName());
				map.put("operationTime", formatter.format(msg.getOperationTime()));
				map.put("operation", msg.getOperation());
				map.put("comment", msg.getComment());
				map.put("nextNodeUsersName", nextNodeName);
				map.put("title", title);
				result.add((HashMap<String, String>) map);
			}
			
			List<String> keys = new ArrayList<String>();
			String key = PrintInterfaceImpl.setPrintData(result);//设置打印数据
			keys.add(key);
			// 返回key
			info.setData(keys);
			return info;
		}catch (Exception e) {
			// TODO: handle exception
			// TODO: handle exception
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			logger.error(e.getMessage(), e);
			return info;
		}
	}

}
